import httpConfig from 'config/http.js'
import request from '@/utils/request.js';
let httpImgApi = httpConfig.BASE_URL;
// let httpImgApi = 'http://localhost:8080' //测试站
// let headers = {'token': localStorage.getItem('token')}

const utils = {
	// 上传图片
	changeGoodsImg(imgUrl, goodsId) {
		return new Promise((resolve, reject) => {
			// const headers = {'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyMDE5MjEwMDc1IiwiZXhwIjoxNjgzNTczMDY2fQ.kPbw-UKMWRqpuMpES8ryLfPGAGqgplTlvTDjPd5JmlE'}
			// const headers = {'token': uni.getStorageSync()('token')}
			uni.uploadFile({
				url: httpImgApi + '/file/updateGoodsImg',
				filePath: imgUrl,
				name: 'file',
				header: {},
				formData: {
					file: imgUrl,
					goodsId
				},
				success: (res) => {
					resolve(JSON.parse(res.data))
				},
				fail: res =>{
					reject(res);
				}
			});
		})
	},
	chagneAvatar(imgUrl, userId) {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: httpImgApi + '/file/updateAvatar',
				filePath: imgUrl,
				name: 'file',
				formData: {
					file: imgUrl,
					userId
				},
				success: (res) => {
					resolve(JSON.parse(res.data))
				},
				fail: res =>{
					reject(res);
				}
			});
		})
	},
	uploadImg(imgUrl) {
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: httpImgApi + '/file/upload',
				filePath: imgUrl,
				name: 'file',
				formData: {
					file: imgUrl
				},
				success: (res) => {
					resolve(JSON.parse(res.data))
				},
				fail: res =>{
					reject(res);
				}
			});
		})
	},
	// 删除照片
	removeImg(imgUrl) {
		return request({
			url: '/file/delete?url=' + imgUrl,
			method: httpConfig.METHODS.DELETE
		})
	},
	
	//获取页面dom元素 
	getRect(selector, _this){
		return new Promise((resolve) => {
			let view = uni.createSelectorQuery().in(_this).select(selector)
			view.fields({
				size: true,
				rect: true,
				scrollOffset: true
			},res => {
				resolve(res)
			}).exec()
		})
	},
	//获取dom元素位置信息
	getDomPosition(selector, _this){
		return new Promise((resolve) => {
			uni.createSelectorQuery().in(_this).select(selector).boundingClientRect(data => {
				resolve(data)
			}).exec();
		})
	},
	getPageVm() {
		const pages = getCurrentPages() // 获取当前页面栈数组，第一个元素为首页，最后一个元素为当前页面
		const page = pages[Math.max(pages.length - 1, 0)] // 要返回到的页面，超过页面栈，则为首页
		return page.$vm; //返回的是页面实例,其中包含所有的方法
	}
	
	
}
module.exports = utils;